package com.ruoyi.service.middlesign.impl;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.controller.middlesign.MiddlesConfig;
import com.ruoyi.controller.middlesign.MiddlesMethod;
import com.ruoyi.controller.middlesign.MiddlesUtils;
import com.ruoyi.mapper.middlesign.MiddlesignMapper;
import com.ruoyi.service.middlesign.MiddlesignService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 中征平台专用公用接口实现类
 * @date 2021/3/15 16:17
 */
@Service
public class MiddlesignServiceImpl implements MiddlesignService {

    @Autowired
    MiddlesignMapper middlesignMapper;

    @Override
    public List<Map<String, Object>> findHxqyList() {
        return middlesignMapper.findHxqyList();
    }

    @Override
    public List<Map<String, Object>> findHistoryData(Map map) {
        return middlesignMapper.findHistoryData(map);
    }
    @Override
    public int insertHistoryData(Map map) {
        return middlesignMapper.insertHistoryData(map);
    }
    @Override
    public Map findHistoryDataById(String id) {
        return middlesignMapper.findHistoryDataById(id);
    }
    @Override
    public int updateHistoryDate(Map map) {
        return middlesignMapper.updateHistoryDate(map);
    }
    @Override
    public BaseResult pushHistoryData(String jsonString, String CREDITCODE, String CREDITNO, String CREDITNAME, String hxqyName, String hxqyCode, String table) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(jsonString);

        //清空B2802的存放文件
        MiddlesUtils.emptyFileTxt(MiddlesConfig.B2802FilePath);
        //定义a用来判断是否需要换行
        int a = 0;
        //字符串拼接
        String pushData = "";
        for (Map map : listObjectFir) {
            //供应商社会信用代码
            String CREDIT_CODE = CREDITCODE;
            //供应商名称
            String CREDIT_NAME = CREDITNAME;
            //授权编码
            String CREDIT_NO = CREDITNO;
            String ORDER_DATE = map.get("ORDER_DATE").toString();
            String ORDERS_SUM_AMT = map.get("ORDERS_SUM_AMT").toString();
            String ORDERS_COUNT = map.get("ORDERS_COUNT").toString();
            String ORDERS_BALANCE = map.get("ORDERS_BALANCE").toString();
            String ORDERS_CANCEL_AMT = map.get("ORDERS_CANCEL_AMT").toString();
            String PAY_LAST_DATE = map.get("PAY_LAST_DATE").toString();
            String YEAR = map.get("YEAR").toString();
            String ORDER_CODE = map.get("Voucher_No").toString();
            //凭证号特殊处理
            String ORDER_AMT = MiddlesUtils.VoucherNumberProcessing(map);
            String PAY_LAST_DATE_SINGLE = map.get("PAY_LAST_DATE_SINGLE").toString();
            String ORDER_BALANCE = map.get("ORDER_BALANCE").toString();
            String ORDER_YEAR = map.get("ORDER_YEAR").toString();
            String COMPANY_CODE = map.get("COMPANY_CODE").toString();
            String mark1 = map.get("mark1").toString();
            pushData += CREDIT_CODE + "┳" + CREDIT_NAME + "┳" + CREDIT_NO + "┳" + ORDERS_SUM_AMT + "┳" + ORDERS_COUNT + "┳";
            pushData += ORDERS_BALANCE + "┳" + ORDERS_CANCEL_AMT + "┳" + PAY_LAST_DATE + "┳" + YEAR + "┳" + mark1 + "┳┳┳" + ORDER_CODE + "┳";
            pushData += ORDER_AMT + "┳" + ORDER_DATE + "┳" + PAY_LAST_DATE_SINGLE + "┳" + ORDER_BALANCE + "┳" + ORDER_YEAR + "┳" + COMPANY_CODE + "┳┳┳";
            //判断是否需要换行
            if (a < listObjectFir.size() - 1) {
                pushData += "\r\n";
            }
            //堆叠B2802文件
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2802FilePath);
            //清空用于下次叠加
            pushData = "";
            a++;
        }

        a = 0;
        List<String> list = middlesignMapper.findYouFukuanWuZhangKuan(CREDITNAME, hxqyName, table);
        for (String years : list) {
            pushData += "\r\n";
            List<Map<String, Object>> list1 = middlesignMapper.findHistoryDataByYear(years, CREDITNAME, table, hxqyName);
            for (Map map : list1) {
                String ORDERS_SUM_AMT = map.get("ORDERS_SUM_AMT").toString();
                String ORDERS_COUNT = map.get("ORDERS_COUNT").toString();
                String ORDERS_BALANCE = map.get("ORDERS_BALANCE").toString();
                String ORDERS_CANCEL_AMT = map.get("ORDERS_CANCEL_AMT").toString();
                String PAY_LAST_DATE = map.get("PAY_LAST_DATE").toString();
                String year = map.get("years").toString();
                String mark1 = map.get("mark1").toString();
                pushData += CREDITCODE + "┳" + CREDITNAME + "┳" + CREDITNO + "┳" + ORDERS_SUM_AMT + "┳" + ORDERS_COUNT + "┳";
                pushData += ORDERS_BALANCE + "┳" + ORDERS_CANCEL_AMT + "┳" + PAY_LAST_DATE + "┳" + year + "┳" + mark1 + "┳┳┳┳┳┳┳┳┳┳┳┳";
                if (a < list1.size() - 1) {
                    pushData += "\r\n";
                }
                a++;
            }
            //堆叠B2802文件
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2802FilePath);
            pushData = "";
            a = 0;
        }

        //文件内容必须不为空方可推送
        if (!MiddlesUtils.getFileText(MiddlesConfig.B2802FilePath).equals("")) {
            //向中征平台发送File
            String responseMsg = MiddlesMethod.sendB2802(MiddlesConfig.B2802FilePath, hxqyCode);
            //判断是否发送成功
            if ((responseMsg).indexOf("成功") != -1) {
                //如果成功的话向数据库更新B2802提交时间
                middlesignMapper.insertHistorPushByCreditNo(CREDITNO, CREDITCODE, CREDITNAME, hxqyName,hxqyCode);
                return BaseResult.build(ResultEnum.SUCCESS);
            } else {
                return BaseResult.build(ResultEnum.FAILURE, responseMsg);
            }
        } else {
            return BaseResult.build(ResultEnum.FAILURE, "文件内容为空");
        }
    }

    @Override
    public int insertAccountsByName(String CREDIT_NAME, String hxqyName) {
        return middlesignMapper.insertAccountsByName(CREDIT_NAME, hxqyName);
    }
    @Override
    public List<Map<String, Object>> findAccountsByName(Map map) {
        return middlesignMapper.findAccountsByName(map);
    }
    @Override
    public BaseResult accountsPushData(String jsonString, String CREDITCODE, String CREDITNAME, String hxqyCode, String type) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(jsonString);
        //清空B2804的存放文件
        MiddlesUtils.emptyFileTxt(MiddlesConfig.B2804FilePath);
        //定义a用来判断是否需要换行
        int a = 0;
        //字符串拼接
        String pushData = "";
        for (Map map : listObjectFir) {
            String CREDIT_CODE = CREDITCODE;
            String CREDIT_NAME = CREDITNAME;
            String COMPANY_CODE = map.get("COMPANY_CODE").toString();
            String ORDER_CODE = map.get("Voucher_No").toString();
            String ORDER_AMT = map.get("ORDER_AMT").toString();
            String ORDER_START_DATE = map.get("ORDER_START_DATE").toString();
            String ORDER_END_DATE = map.get("ORDER_END_DATE").toString();
            String INVOICE_NO = map.get("INVOICE_NO").toString();
            String ORDER_NO = map.get("ORDER_NO").toString();
            String CONTRACT_NO = map.get("CONTRACT_NO").toString();
            String ORDER_CURCD = map.get("ORDER_CURCD").toString();
            String ORDER_YEAR = map.get("ORDER_YEAR").toString();
            pushData += CREDIT_CODE + "┳" + CREDIT_NAME + "┳" + COMPANY_CODE + "┳" + ORDER_CODE + "┳" + ORDER_AMT + "┳";
            pushData += ORDER_START_DATE + "┳" + ORDER_END_DATE + "┳" + INVOICE_NO + "┳" + ORDER_NO + "┳" + CONTRACT_NO + "┳";
            pushData += ORDER_CURCD + "┳" + ORDER_YEAR + "┳┳┳┳┳";
            if (a < listObjectFir.size() - 1) {
                pushData += "\r\n";
            }
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2804FilePath);
            pushData = "";
            a++;
        }

        if (!MiddlesUtils.getFileText(MiddlesConfig.B2804FilePath).equals("")) {
            String responseMsg = MiddlesMethod.sendB2804(MiddlesConfig.B2804FilePath, hxqyCode);
            if (responseMsg.indexOf("成功") != -1) {
                if ("time".equals(type)) {
                    //如果是定时推送，则更新B2804推送时间
                    middlesignMapper.updateAccountsPushByCreditNo(CREDITCODE);
                    return BaseResult.build(ResultEnum.SUCCESS);
                } else {
                    return BaseResult.build(ResultEnum.SUCCESS);
                }
            } else {
                return BaseResult.build(ResultEnum.FAILURE, responseMsg);
            }
        } else {
            return BaseResult.build(ResultEnum.FAILURE, "文件内容为空");
        }
    }

    @Override
    public int insertPaymentByName(String CREDIT_NAME, String hxqyName) {
        return middlesignMapper.insertPaymentByName(CREDIT_NAME, hxqyName);
    }
    @Override
    public List<Map<String, Object>> findPaymentByName(Map map) {
        return middlesignMapper.findPaymentByName(map);
    }
    @Override
    public BaseResult paymentPushData(String jsonString, String CREDITCODE, String CREDITNAME, String hxqyCode, String type) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(jsonString);
        //清空B2805的存放文件
        MiddlesUtils.emptyFileTxt(MiddlesConfig.B2805FilePath);
        //定义a用来判断是否需要换行
        int a = 0;
        //字符串拼接
        String pushData = "";
        for (Map map : listObjectFir) {
            String CREDIT_CODE = CREDITCODE;
            String CREDIT_NAME = CREDITNAME;
            String PAY_CODE = map.get("Voucher_No").toString();
            String PAY_CURCD = map.get("PAY_CURCD").toString();
            String PAY_AMT = map.get("PAY_AMT").toString();
            String PAY_DATE = map.get("PAY_DATE").toString();
            String PAY_YEAR = map.get("PAY_YEAR").toString();
            String COMPANY_CODE = map.get("COMPANY_CODE").toString();
            String ORDER_CODE = map.get("ORDER_CODE").toString();
            String MARK1 = map.get("MARK1").toString();
            pushData += CREDIT_CODE + "┳" + CREDIT_NAME + "┳" + PAY_CODE + "┳" + PAY_CURCD + "┳" + PAY_AMT + "┳";
            pushData += PAY_DATE + "┳┳┳┳┳┳" + PAY_CODE + "┳" + PAY_YEAR + "┳" + COMPANY_CODE + "┳" + ORDER_CODE + "┳";
            pushData += MARK1 + "┳┳┳┳";
            if (a < listObjectFir.size() - 1) {
                pushData += "\r\n";
            }
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2805FilePath);
            pushData = "";
            a++;
        }

        if (!MiddlesUtils.getFileText(MiddlesConfig.B2805FilePath).equals("")) {
            String responseMsg = MiddlesMethod.sendB2805(MiddlesConfig.B2805FilePath, hxqyCode);
            if ((responseMsg).indexOf("成功") != -1) {
                if ("time".equals(type)) {
                    //如果是定时推送，则更新B2805推送时间
                    middlesignMapper.updatePaymentPushByCreditNo(CREDITCODE);
                    return BaseResult.build(ResultEnum.SUCCESS);
                } else {
                    return BaseResult.build(ResultEnum.SUCCESS);
                }
            } else {
                return BaseResult.build(ResultEnum.FAILURE, responseMsg);
            }
        } else {
            return BaseResult.build(ResultEnum.FAILURE, "文件内容为空");
        }
    }

    @Override
    public int insertStockSupplierAccount(String CREDIT_NAME) {
        return middlesignMapper.insertStockSupplierAccount(CREDIT_NAME);
    }
    @Override
    public List<Map<String, Object>> findStockSupplierAccount(String CREDIT_NAME) {
        return middlesignMapper.findStockSupplierAccount(CREDIT_NAME);
    }
    @Override
    public BaseResult supplierPush(String data, String CREDITCODE, String hxqyCode, String type) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(data);
        int a = 0;
        MiddlesUtils.emptyFileTxt(MiddlesConfig.B2806FilePath);
        //字符串拼接
        String pushData = "";
        List<String> list = new ArrayList<String>();
        for (Map map : listObjectFir) {
            String id = map.get("id").toString();
            list.add(id);
            String COMPANY_NAME = map.get("COMPANY_NAME").toString();
            String COMPANY_CODE = map.get("COMPANY_CODE").toString();
            String CREDIT_NAME = map.get("CREDIT_NAME").toString();
            String CREDIT_CODE = map.get("CREDIT_CODE").toString();
            String BACK_ACCOUNT_NAME = map.get("BACK_ACCOUNT_NAME").toString();
            String BACK_ACCOUNT_NO = map.get("BACK_ACCOUNT_NO").toString();
            String OPEN_BANK_NAME = map.get("OPEN_BANK_NAME").toString();
            pushData += COMPANY_NAME + "┳" + COMPANY_CODE + "┳" + CREDIT_NAME + "┳" + CREDIT_CODE + "┳" + BACK_ACCOUNT_NAME + "┳";
            pushData += BACK_ACCOUNT_NO + "┳" + OPEN_BANK_NAME + "┳┳┳┳┳";
            if (a < listObjectFir.size() - 1) {
                pushData += "\r\n";
            }
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2806FilePath);
            pushData = "";
            a++;
        }

        if (!MiddlesUtils.getFileText(MiddlesConfig.B2806FilePath).equals("")) {
            String responseMsg = MiddlesMethod.sendB2806(MiddlesConfig.B2806FilePath, hxqyCode);
            if (responseMsg.indexOf("成功") != -1) {
                //成功后将已经更新的数据做记号，避免每日又重新发送
                middlesignMapper.updateStockSupplierAccountIsSend(list);
                if ("time".equals(type)) {
                    //成功后更新历史数据提交时间
                    middlesignMapper.updateSupplierByCreditNo(CREDITCODE);
                }
                return BaseResult.build(ResultEnum.SUCCESS);
            } else {
                return BaseResult.build(ResultEnum.FAILURE, responseMsg);
            }
        } else {
            return BaseResult.build(ResultEnum.FAILURE, "文件内容为空");
        }
    }

    @Override
    public int insertReconciliationData(String CREDIT_NAME, String hxqyName) {
        return middlesignMapper.insertReconciliationData(CREDIT_NAME, hxqyName);
    }
    @Override
    public List<Map<String, Object>> findReconciliationData(Map map) {
        return middlesignMapper.findReconciliationData(map);
    }
    @Override
    public BaseResult reconciliationDataPush(String jsonString, String CREDITCODE, String CREDITNAME, String hxqyCode, String type) {
        List<Map<String, String>> listObjectFir = (List<Map<String, String>>) JSONArray.parse(jsonString);
        int a = 0;
        MiddlesUtils.emptyFileTxt(MiddlesConfig.B2807FilePath);
        //字符串拼接
        String pushData = "";
        for (Map map : listObjectFir) {
            String CREDIT_CODE = CREDITCODE;
            String CREDIT_NAME = CREDITNAME;
            String ACCOUNT_ADJUSTMENT_NO = map.get("Voucher_No").toString();
            String ACCOUNT_ADJUSTMENT_DATE = map.get("ACCOUNT_ADJUSTMENT_DATE").toString();
            String ACCOUNT_ADJUSTMENT_AMT = map.get("ACCOUNT_ADJUSTMENT_AMT").toString();
            String CURCD = map.get("CURCD").toString();
            String ADJUSTMENT_DIRECTION = map.get("ADJUSTMENT_DIRECTION").toString();
            String MARK2 = map.get("MARK2").toString();
            String MARK3 = map.get("Voucher_No").toString();
            pushData += CREDIT_CODE + "┳" + CREDIT_NAME + "┳" + ACCOUNT_ADJUSTMENT_NO + "┳" + ACCOUNT_ADJUSTMENT_DATE + "┳" + ACCOUNT_ADJUSTMENT_AMT + "┳";
            pushData += CURCD + "┳" + ADJUSTMENT_DIRECTION + "┳┳" + MARK2 + "┳" + MARK3 + "┳┳";
            if (a < listObjectFir.size() - 1) {
                pushData += "\r\n";
            }
            MiddlesUtils.Additional(pushData, MiddlesConfig.B2807FilePath);
            pushData = "";
            a++;
        }

        if (!MiddlesUtils.getFileText(MiddlesConfig.B2807FilePath).equals("")) {
            String responseMsg = MiddlesMethod.sendB2807(MiddlesConfig.B2807FilePath, hxqyCode);
            if (responseMsg.indexOf("成功") != -1) {
                return BaseResult.build(ResultEnum.SUCCESS);
            } else {
                return BaseResult.build(ResultEnum.FAILURE, responseMsg);
            }
        } else {
            return BaseResult.build(ResultEnum.FAILURE, "文件内容为空");
        }
    }


    @Override
    public Map getStockDataAccountsById(String id, String table) {
        return middlesignMapper.getStockDataAccountsById(id,table);
    }
    @Override
    public int accountsSave(Map map) {
        return middlesignMapper.accountsSave(map);
    }

    @Override
    public Map getStockDataPaymentById(String id, String table) {
        return middlesignMapper.getStockDataPaymentById(id,table);
    }

    @Override
    public int paymentSave(Map map) {
        return middlesignMapper.paymentSave(map);
    }

    @Override
    public Map getSupplierAccountById(String id) {
        return middlesignMapper.getSupplierAccountById(id);
    }

    @Override
    public int stockSupplierAccountSave(Map map) {
        return middlesignMapper.stockSupplierAccountSave(map);
    }

    @Override
    public Map getReconciliationById(String id, String table) {
        return middlesignMapper.getReconciliationById(id, table);
    }

    @Override
    public int reconciliationSave(Map map) {
        return middlesignMapper.reconciliationSave(map);
    }
}
